package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.cardmanagement.util.CardStock;
import com.turing.post.cardmanagement.util.CardStockDao;
import com.turing.post.cardmanagement.util.IssueCardStockDetail;
import com.turing.post.cardmanagement.util.IssueCardStockDetailDao;
import com.turing.post.cardmanagement.util.MakeCardControl;
import com.turing.post.cardmanagement.util.MakeCardControlDao;
import com.turing.post.cardmanagement.util.SaleCardStockDetail;
import com.turing.post.cardmanagement.util.SaleCardStockDetailDao;
import com.turing.post.cardmanagement.util.TransferInOutStockOrder;
import com.turing.post.cardmanagement.util.TransferInOutStockOrderBusiness;
import com.turing.post.cardmanagement.util.TransferInOutStockOrderDao;
import com.turing.post.cardmanagement.util.TransferInOutStockOrderDetail;
import com.turing.post.cardmanagement.util.TransferInOutStockOrderDetailDao;
import com.turing.post.cardmanagement.util.TransferOrderActionDetail;
import com.turing.post.cardmanagement.util.TransferOrderActionDetailDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 卡调拨管理业务
 * 
 * @author YangShuo
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.TransferInOutStockOrderBusinessImpl")
@Remote
@Local
public class TransferInOutStockOrderBusinessImpl implements
		TransferInOutStockOrderBusiness {

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao SysOpUserDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.CardStockDaoImpl")
	private CardStockDao CardStockDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.TransferInOutStockOrderDetailDaoImpl")
	private TransferInOutStockOrderDetailDao TransferInOutStockOrderDetailDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.TransferInOutStockOrderDaoImpl")
	private TransferInOutStockOrderDao TransferInOutStockOrderDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.MakeCardControlDaoImpl")
	private MakeCardControlDao MakeCardControlDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.IssueCardStockDetailDaoImpl")
	private IssueCardStockDetailDao IssueCardStockDetailDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.SaleCardStockDetailDaoImpl")
	private SaleCardStockDetailDao SaleCardStockDetailDaoImpl;

	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao SysBranchDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.TransferOrderActionDetailDaoImpl")
	private TransferOrderActionDetailDao TransferOrderActionDetailDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao IssueCompanyDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	/**
	 * @新增调拨单
	 * @param TransferInOutStockOrder
	 * @param detaillist
	 */
	@Override
	public BigInteger newTransferInOutStockOrderWithDetail(
			PCPSWebInput webInput,
			TransferInOutStockOrder transferInOutStockOrder,
			List<TransferInOutStockOrderDetail> detaillist)
			throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "状态不是调出在途或拒绝");
		}
		// 检查调拨信息
		this.checkTransferInOutStockOrderinput(transferInOutStockOrder);
		// 该置空的置空
		transferInOutStockOrder.setCancelTransferOrderId(null);
		transferInOutStockOrder.setConfirmOperId(null);
		transferInOutStockOrder.setConfirmTime(null);
		transferInOutStockOrder.setConfirmCancelRemark(null);

		// 设置status为调出录入
		transferInOutStockOrder
				.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTTYPEDIN);
		// 产生主键和时间
		BigInteger id = pcpsCommonDaoImpl.getUniqueIntegerKey();
		Date now = pcpsCommonDaoImpl.getDatabaseSysTime();
		transferInOutStockOrder.setTransferOrderId(id);
		transferInOutStockOrder.setTranTime(now);

		// 业务单号
		transferInOutStockOrder.setOrderNumber(pcpsCommonDaoImpl
				.getOrderNumber(null, webInput.getOperBranchNo()));

		// 插入调拨单
		TransferInOutStockOrderDaoImpl
				.insertTransferInOutStockOrder(transferInOutStockOrder);

		// 将明细数据插入顺便将库存表中状态改为在途
		String sysbranch = transferInOutStockOrder.getFromIssueBranchNo() == null ? transferInOutStockOrder
				.getFromSaleBranchNo()
				: transferInOutStockOrder.getFromIssueBranchNo();
		this.insertTransferInOutStockOrderDetailLis(sysbranch, detaillist,
				transferInOutStockOrder);

		// 插入调拨单迁移明细
		TransferOrderActionDetail toad = new TransferOrderActionDetail();
		toad.setTransferOrderId(id);
		toad.setActionTime(now);
		toad.setOperId(webInput.getOperId());
		toad
				.setINITIALSTATUS(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_CREATE);
		toad
				.setENDSTATUS(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_CREATE);
		toad
				.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_CREATE);
		toad.setRemark("调拨单创建");
		TransferOrderActionDetailDaoImpl.insertTransferOrderActionDetail(toad);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建调拨单", "TransferOrderId:"
						+ transferInOutStockOrder.getTransferOrderId());
		return transferInOutStockOrder.getTransferOrderId();
	}

	/**
	 * @出库方取消调拨单,单号，操作员
	 * @param TransferInOutStockOrderId
	 * @param UserId
	 * 
	 */
	@Override
	public void AuthTransferInOutStockOrderByFromBranch(PCPSWebInput webInput,
			BigInteger TransferInOutStockOrderId, BigInteger UserId)
			throws PCPSApplicationException {
		// 只有状态为调出录入和调出在途的调拨单允许取消，目前设定调必须发起人取消。
		TransferInOutStockOrder tios = TransferInOutStockOrderDaoImpl
				.getTransferInOutStockOrderByTransferInOutStockOrderId(TransferInOutStockOrderId);
		if (!tios
				.getTransferStatus()
				.equals(
						TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE)
				&& !tios
						.getTransferStatus()
						.equals(
								TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "状态不是调出在途或拒绝");
		String operbranch = SysOpUserDaoImpl.getSysOpUserByOperId(UserId)
				.getOperBranchNo();;
		if (!(!operbranch.equals(tios.getFromIssueBranchNo()) ^ !operbranch
				.equals(tios.getFromSaleBranchNo())))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "操作员不属于此机构");
		if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderId, UserId)>0){
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "取消人和创建人不能是同一个人!");
		}
		// 记录状态迁移明细
		TransferOrderActionDetail toad = new TransferOrderActionDetail();
		toad.setTransferOrderId(tios.getTransferOrderId());
		toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		toad.setOperId(UserId);
		toad.setINITIALSTATUS(tios.getTransferStatus());
		toad
				.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTCANCEL);
		toad
				.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_OUTCANCEL);
		toad.setRemark("取消调出");
		TransferOrderActionDetailDaoImpl.insertTransferOrderActionDetail(toad);

		// 修改调拨单状态
		tios
				.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTCANCEL);
		TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tios);

		// 取得号段明细，然后逐条记录
		List<TransferInOutStockOrderDetail> detaillist = TransferInOutStockOrderDetailDaoImpl
				.getTransferInOutStockOrderDetail(TransferInOutStockOrderId);
		// 记录出入库明细
//		if (SysBranchDaoImpl.getSysBranchByBranchNo(operbranch).getBranchType()
//				.equals(SysBranch.SYSBRANCH_ISSUEBRANCH))
//			this
//					.insertIssueCardStockDetailList(
//							"调出取消",
//							UserId,
//							operbranch,
//							detaillist,
//							tios,
//							IssueCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
//							-1);
//		else
//			this.insertSaleCardStockDetailList("调出取消", UserId, operbranch,
//					detaillist, tios,
//					SaleCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
//					-1);
		// 将涉及的卡状态改为正常
		TransferInOutStockOrderDetail tisod = null;
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			CardStockDaoImpl.updateCardStock(tisod.getBatchNo(), tisod
					.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
					CardStock.CARDSTOCK_STATUS_NORMAL);
		}
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "出库方取消调拨单", "TransferOrderId:"
						+ tios.getTransferOrderId());
	}

	/**
	 * @出库方审批新增调拨单,单号，审批人，是否同意true代表同意false代表打回
	 * @param TransferInOutStockOrderId
	 * @param UserId
	 * @param ispass
	 */
	@Override
	public void AuthTransferInOutStockOrderByFromBranch(PCPSWebInput webInput,
			BigInteger TransferInOutStockOrderId, BigInteger UserId,
			boolean ispass) throws PCPSApplicationException {
		// 根据单号得到对象
		TransferInOutStockOrder tios = TransferInOutStockOrderDaoImpl
				.getTransferInOutStockOrderByTransferInOutStockOrderId(TransferInOutStockOrderId);
		if (!tios
				.getTransferStatus()
				.equals(
						TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTTYPEDIN))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "状态不是调出录入");
		// 核对操作员机构
		String operbranch = SysOpUserDaoImpl.getSysOpUserByOperId(UserId)
				.getOperBranchNo();
		// PCPSUtil.Log("operbranch==="+operbranch+"tios.getFromIssueBranchNo()=="+tios.getFromIssueBranchNo()+"tios.getFromSaleBranchNo()=="+tios.getFromSaleBranchNo());
		if (!(!operbranch.equals(tios.getFromIssueBranchNo()) ^ !operbranch
				.equals(tios.getFromSaleBranchNo())))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "操作员不属于此机构");
		
		// 取得号段明细，然后逐条记录
		List<TransferInOutStockOrderDetail> detaillist = TransferInOutStockOrderDetailDaoImpl
				.getTransferInOutStockOrderDetail(TransferInOutStockOrderId);
		if (ispass) {
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderId, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "审核人和创建人不能是同一个人!请合理分配角色!");
			}
			// 审批通过，将状态改为调出在途，记录出入库明细，状态迁移明细
			tios
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE);
			tios.setIssueOperId(UserId);

			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tios);

			// 记录状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tios.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTTYPEDIN);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_OUTPASS);
			toad.setRemark("调出审批通过");
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);

			// 记录出入库明细,发卡商机构调用发卡商机构方法，销售机构调用销售机构方法
//			if (SysBranchDaoImpl.getSysBranchByBranchNo(operbranch)
//					.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH))
//				this
//						.insertIssueCardStockDetailList(
//								"调拨出库确认",
//								UserId,
//								operbranch,
//								detaillist,
//								tios,
//								IssueCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
//								1);
//			else
//				this
//						.insertSaleCardStockDetailList(
//								"调拨出库确认",
//								UserId,
//								operbranch,
//								detaillist,
//								tios,
//								SaleCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
//								1);
		} else {
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderId, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "取消人和创建人不能是同一个人!请合理分配角色!");
			}
			// 审批不通过，将状态改为取消调出，将取消ID设为自己。不记录出入库明细，记录状态迁移明细，将数据库中卡片状态修改为正常
			tios
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTCANCEL);
			tios.setCancelTransferOrderId(tios.getTransferOrderId());
			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tios);
			// 修改库存卡表将所有卡状态置为正常
			this.updateTransferInOutStockOrderDetailForStatus(operbranch,
					detaillist);
			// 记录一条状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tios.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTTYPEDIN);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTCANCEL);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_OUTCANCEL);
			toad.setRemark("调出审批不通过");
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);
			// 日志记录
			sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
					SysOperLog.operType_Edit, "出库方审批新增调拨单", "TransferOrderId:"
							+ tios.getTransferOrderId());
		}
	}

	/**
	 * @接收方接收调拨单,单号，接收人，是否同意true代表同意接收false代表拒收
	 * @param TransferInOutStockOrderId
	 * @param UserId
	 * @param ispass
	 */
	@Override
	public void AuthTransferInOutStockOrderByToBranch(PCPSWebInput webInput,
			BigInteger TransferInOutStockOrderId, BigInteger UserId,
			String ConfirmCancelRemark, boolean ispass)
			throws PCPSApplicationException {
		// 根据单号得到对象
		TransferInOutStockOrder tios = TransferInOutStockOrderDaoImpl
				.getTransferInOutStockOrderByTransferInOutStockOrderId(TransferInOutStockOrderId);
		tios.setConfirmOperId(UserId);
		tios.setConfirmTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		tios.setConfirmCancelRemark(ConfirmCancelRemark);		
		if (!tios
				.getTransferStatus()
				.equals(
						TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "状态不是调出在途");
		// 核对操作员机构
		String operbranch = SysOpUserDaoImpl.getSysOpUserByOperId(UserId)
				.getOperBranchNo();
		if (!(!operbranch.equals(tios.getToIssueBranchNo()) ^ !operbranch
				.equals(tios.getToSaleBranchNo())))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "操作员不属于此机构或记录异常");
		if (ispass) {
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderId, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "接收人和（创建人或调出审核人）不能是同一个人!请合理分配角色!");
			}
			// 接收通过，将状态改为接收，记录状态迁移明细
			tios
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE);
			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tios);

			// 记录状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tios.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_RECEIVE);
			toad.setRemark("接收");
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);

		} else {
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderId, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "接收审核人和（创建人或调出审核人或接收人）不能是同一个人!请合理分配角色!");
			}
			// 接收不通过，将状态改为拒收，记录状态迁移明细
			tios
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION);
			tios.setConfirmOperId(UserId);
			tios.setConfirmTime(pcpsCommonDaoImpl.getDatabaseSysTime());

			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tios);

			// 取得号段明细，然后逐条记录
			List<TransferInOutStockOrderDetail> detaillist = TransferInOutStockOrderDetailDaoImpl
					.getTransferInOutStockOrderDetail(TransferInOutStockOrderId);
			
			// 将涉及的卡状态改为正常
			TransferInOutStockOrderDetail tisod = null;
			for (int i = 0; i < detaillist.size(); i++) {
				tisod = detaillist.get(i);
				CardStockDaoImpl.updateCardStock(tisod.getBatchNo(), tisod
						.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
						CardStock.CARDSTOCK_STATUS_NORMAL);
			}
			
			// 记录状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tios.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_OUTONPASSAGE);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_REFUSE);
			toad.setRemark("拒收");
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);
			// 日志记录
			sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
					SysOperLog.operType_Edit, "接收方接受调拨单", "TransferOrderId:"
							+ tios.getTransferOrderId());
		}
	}

	/**
	 * @发卡商机构批量插入出入库明细方法,备注，操作员，机构号，调拨单明细，调拨单对象，状态，正负标志
	 * 
	 */
	private void insertIssueCardStockDetailList(String remark,
			BigInteger UserId, String sysbranch,
			List<TransferInOutStockOrderDetail> detaillist,
			TransferInOutStockOrder tios, String status, int pn)
			throws PCPSApplicationException {
		TransferInOutStockOrderDetail tisod = null;
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			IssueCardStockDetail ics = new IssueCardStockDetail();

			MakeCardControl mcc = MakeCardControlDaoImpl
					.getMakeCardControlByBatchNo(tisod.getBatchNo());

			ics.setIoOrderId(tios.getTransferOrderId());
			ics.setIssueBranchNo(sysbranch);
			ics.setTranDate(IssueCompanyDaoImpl
					.getIssueCompanyByIssueCompanyNo(
							PCPSUtil.getICNoByBranchNo(sysbranch))
					.getTranDate());
			ics.setTrantime(pcpsCommonDaoImpl.getDatabaseSysTime());
			ics.setTranType(status);
			ics.setCardFaceId(mcc.getCardFaceId());
			// ics.setCardClassicId(mcc.getCardClassicId());
			// ics.setCardParValueId(mcc.getCardParValueId());
			ics.setBatchNo(mcc.getBatchNo());
			ics.setCardStartSeqNo(tisod.getCardStartSeqNo());
			ics.setCardEndSeqNo(tisod.getCardEndSeqNo());
			ics.setOrderCount(tisod.getOrderCount() * pn);
			ics.setOperId(UserId);
			ics.setRemark(remark);
			IssueCardStockDetailDaoImpl.insert(ics);
		}
	}

	/**
	 * @销售机构批量插入出入库明细方法,备注，操作员，机构号，调拨单明细，调拨单对象，状态，正负标志
	 * 
	 */
	private void insertSaleCardStockDetailList(String remark,
			BigInteger UserId, String sysbranch,
			List<TransferInOutStockOrderDetail> detaillist,
			TransferInOutStockOrder tios, String status, int pn)
			throws PCPSApplicationException {
		TransferInOutStockOrderDetail tisod = null;
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			SaleCardStockDetail ics = new SaleCardStockDetail();

			MakeCardControl mcc = MakeCardControlDaoImpl
					.getMakeCardControlByBatchNo(tisod.getBatchNo());

			ics.setIoOrderId(tios.getTransferOrderId());
			ics.setSaleBranchNo(sysbranch);
			ics.setTranDate(IssueCompanyDaoImpl
					.getIssueCompanyByIssueCompanyNo(
							PCPSUtil.getICNoByBranchNo(sysbranch))
					.getTranDate());
			ics.setTrantime(pcpsCommonDaoImpl.getDatabaseSysTime());
			ics.setTranType(status);
			ics.setCardFaceId(mcc.getCardFaceId());
			// ics.setCardClassicId(mcc.getCardClassicId());
			// ics.setCardParValueId(mcc.getCardParValueId());
			ics.setBatchNo(mcc.getBatchNo());
			ics.setCardStartSeqNo(tisod.getCardStartSeqNo());
			ics.setCardEndSeqNo(tisod.getCardEndSeqNo());
			ics.setOrderCount(tisod.getOrderCount() * pn);
			ics.setOperId(tios.getOperNo());
			ics.setRemark(remark);
			SaleCardStockDetailDaoImpl.insert(ics);
		}
	}

	/**
	 * @批量插入调拨单明细方法
	 * 
	 */
	private void insertTransferInOutStockOrderDetailLis(String sysbranch,
			List<TransferInOutStockOrderDetail> detaillist,
			TransferInOutStockOrder tiod) throws PCPSApplicationException {
		TransferInOutStockOrderDetail tisod = null;
		HashMap<String, String> hm = new HashMap<String, String>();
		int allcount = 0;
		int j=1;
		HashMap<String, String> startseqmap = new HashMap<String, String>();
		HashMap<String, String> endseqmap = new HashMap<String, String>();
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			tisod.setTransferInOutStockOrderId(tiod.getTransferOrderId());
			tisod.setOrderItemSeqNo(i);
			if (PCPSUtil.isEmpty(tisod.getCardStartSeqNo())) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "第" + j + "行起始号为空!");
			}
			if (PCPSUtil.isEmpty(tisod.getCardEndSeqNo())) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "第" + j + "行终止号为空!");
			}
			
			if (startseqmap.get(tisod.getCardStartSeqNo().trim()) != null) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			if (endseqmap.get(tisod.getCardEndSeqNo().trim()) != null) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行终止号已经重复录入!");
			}

			if (endseqmap.get(tisod.getCardStartSeqNo().trim()) != null) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			if (startseqmap.get(tisod.getCardEndSeqNo().trim()) != null) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行终止号已经重复录入!");
			}
			if (isVoucherNo(tisod.getCardStartSeqNo().trim(), startseqmap).endsWith(
					"error")) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			if (isVoucherNo(tisod.getCardEndSeqNo().trim().trim(), startseqmap).endsWith("error")) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			if (isVoucherNo(tisod.getCardStartSeqNo().trim(), startseqmap).endsWith("error")) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			if (isVoucherNo(tisod.getCardEndSeqNo().trim(), endseqmap).endsWith("error")) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "第" + j + "行起始号已经重复录入!");
			}
			startseqmap.put(tisod.getCardStartSeqNo().trim(), tisod.getCardEndSeqNo().trim());
			endseqmap.put(tisod.getCardEndSeqNo().trim(), tisod.getCardStartSeqNo().trim());
			j++;
			
			// 验证号段合法性,如果截取和转化出问题说明号段异常，如果张数不符，也不允许继续
			try {
				if (tisod.getCardEndSeqNo().trim().length() < 18) {
					tisod.setOrderCount((Integer.parseInt(tisod
							.getCardEndSeqNo().substring(6, 12))
							- Integer.parseInt(tisod.getCardStartSeqNo()
									.substring(6, 12)) + 1));
				} else {
					tisod.setOrderCount((Integer.parseInt(tisod
							.getCardEndSeqNo().substring(12, 18))
							- Integer.parseInt(tisod.getCardStartSeqNo()
									.substring(12, 18)) + 1));
				}
			} catch (Exception e) {
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "卡号段" + tisod.getCardStartSeqNo() + "到"
								+ tisod.getCardEndSeqNo() + "有误，"
								+ e.getMessage());
			}
			// 将每个明细的count累加用作以后判断卡序号段是否重叠
			allcount = allcount + tisod.getOrderCount();
			List<CardStock> cardstocklist = CardStockDaoImpl
					.getCardStockListCountBySysbranchnoNoAndStatusAndCredseqno(
							tisod.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
							CardStock.CARDSTOCK_STATUS_NORMAL, sysbranch);
			if (tisod.getOrderCount() != cardstocklist.size()) {
				HashMap<String, Integer> cardMap = CardStockDaoImpl
						.getCardStockCountBySysbranchnoNoAndStatusAndCredseqno(
								tisod.getCardStartSeqNo(), tisod
										.getCardEndSeqNo(),
								CardStock.CARDSTOCK_STATUS_NORMAL, sysbranch);
				// 验证输入卡号是否连续，且状态正常 提示不清晰
				if (cardMap.size() < 1) {
					throw new CardManaApplicationException(
							PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
							"", "库存中不存在卡序号段" + tisod.getCardStartSeqNo() + "至"
									+ tisod.getCardEndSeqNo() + "的卡!");
				} else {
					throw new CardManaApplicationException(
							PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
							"", "卡号段状态异常:" + CardStock.getErrorStatus(cardMap));

				}

			}
			for (int k = 0; k < cardstocklist.size(); k++) {
				CardStock cs = cardstocklist.get(k);
				hm.put(cs.getCardSeqNo(), cs.getCardSeqNo());
			}

			if (PCPSUtil.isEmpty(tisod.getCardStartSeqNo())
					|| PCPSUtil.isEmpty(tisod.getCardEndSeqNo()))
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "起止卡号不能为空");
			// 截取到Batchno
			if (tisod.getCardStartSeqNo().trim().length() < 18) {
				tisod.setBatchNo(PCPSUtil.getICNoByBranchNo(sysbranch)
						+ tisod.getCardStartSeqNo().substring(0, 6));
			} else {
				tisod.setBatchNo(tisod.getCardStartSeqNo().substring(0, 12));
			}
			tisod.setStartCardNo(CardStockDaoImpl.getCardStockByCardseqno(
					tisod.getCardStartSeqNo()).getCardNo());
			tisod.setEndCardNo(CardStockDaoImpl.getCardStockByCardseqno(
					tisod.getCardEndSeqNo()).getCardNo());
			// 插入数据
			TransferInOutStockOrderDetailDaoImpl
					.insertTransferInOutStockOrderDetail(tisod);
			// 修改状态为在途
			CardStockDaoImpl.updateCardStock(tisod.getBatchNo(), tisod
					.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
					CardStock.CARDSTOCK_STATUS_INTRANSIT);

		}
		// PCPSUtil.Log("hm.size()==="+hm.size()+"allcount=="+allcount);
		if (hm.size() != allcount)
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "卡序号段有重叠现象");
	}

	// 检验调拨单数据方法
	public void checkTransferInOutStockOrderinput(TransferInOutStockOrder tiso)
			throws PCPSApplicationException {
		// 检查机构信息
		boolean b = PCPSUtil.isEmpty(tiso.getFromIssueBranchNo())
				^ PCPSUtil.isEmpty(tiso.getFromSaleBranchNo());
		if (!b) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "出库机构号码不能为空");
		}
		b = PCPSUtil.isEmpty(tiso.getToIssueBranchNo())
				^ PCPSUtil.isEmpty(tiso.getToSaleBranchNo());
		if (!b) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "入库机构号码不能为空");
		}

		// if(PCPSUtil.isEmpty(tiso.getTransferStatus())||
		// (tiso.getTransferStatus().equals(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_ONPASSAGE)&&
		// tiso.getTransferStatus().equals(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE)&&
		// tiso.getTransferStatus().equals(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION)))
		// throw new CardManaApplicationException(
		// CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
		// "status为空或不合法");
		// 核对操作员信息
		String opersysbranch = SysOpUserDaoImpl.getSysOpUserByOperId(
				tiso.getOperNo()).getOperBranchNo();
		// PCPSUtil.Log("opersysbranch==="+opersysbranch+"tiso.getFromIssueBranchNo()=="+tiso.getFromIssueBranchNo()+"tiso.getFromSaleBranchNo()=="+tiso.getFromSaleBranchNo());
		if (PCPSUtil.isEmpty(opersysbranch)
				|| (!(tiso.getFromIssueBranchNo() != null && !tiso
						.getFromIssueBranchNo().equals(opersysbranch)) ^ !(tiso
						.getFromSaleBranchNo() != null && !tiso
						.getFromSaleBranchNo().equals(opersysbranch))))

			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "非法操作员");
	}

	/**
	 * @拒绝调拨单审批
	 * @param TransferInOutStockOrderID
	 * @param operno
	 * @param remark
	 */
	@Override
	public void regectionTransferInOutStockOrder(PCPSWebInput webInput,
			BigInteger TransferInOutStockOrderID, BigInteger UserId,
			String remark) throws PCPSApplicationException {
		// 根据主键找到对象
		TransferInOutStockOrder tiso = TransferInOutStockOrderDaoImpl
				.getTransferInOutStockOrderByTransferInOutStockOrderId(TransferInOutStockOrderID);

		// 跟据接收人员取得机构号对比接收是否正确
		String sysbranch = SysOpUserDaoImpl.getSysOpUserByOperId(UserId)
				.getOperBranchNo();
		if (!(!tiso.getToIssueBranchNo().equals(sysbranch) ^ !tiso
				.getToSaleBranchNo().equals(sysbranch))) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "操作员并非接收方操作员");
		}
		if(UserId!=null){
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderID, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "拒绝人和（创建人或调出审核人或接收人）不能是同一个人!请合理分配角色!");
			}
		}
		// 修改卡库存，改变卡状态，不改变卡所属机构
		List<TransferInOutStockOrderDetail> detaillist = TransferInOutStockOrderDetailDaoImpl
				.getTransferInOutStockOrderDetail(TransferInOutStockOrderID);
		this
				.updateTransferInOutStockOrderDetailForStatus(sysbranch,
						detaillist);

		// 修改调拨表，做打回确认。
		tiso
				.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION);
		tiso.setConfirmOperId(UserId);
		tiso.setConfirmTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		tiso.setConfirmCancelRemark(remark);
		TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tiso);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "拒绝调拨单", "TransferOrderId:"
						+ tiso.getTransferOrderId());

	}

	/**
	 * @接收调拨单审批方法
	 * @param TransferInOutStockOrderID
	 * @param operno
	 * @param remark
	 * @param ispass
	 *            true 通过false 不通过
	 */
	@Override
	public void revelveTransferInOutStockOrderApproval(PCPSWebInput webInput,
			BigInteger TransferInOutStockOrderID, BigInteger UserId,
			String remark, boolean ispass) throws PCPSApplicationException {
		// 根据主键找到对象
		TransferInOutStockOrder tiso = TransferInOutStockOrderDaoImpl
				.getTransferInOutStockOrderByTransferInOutStockOrderId(TransferInOutStockOrderID);
		if (!tiso
				.getTransferStatus()
				.equals(
						TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE))
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "状态不是接收");
		if (!PCPSUtil.isEmpty(remark))
			tiso.setConfirmCancelRemark(remark);
		// 跟据接收人员取得机构号对比接收是否正确
		String sysbranch = SysOpUserDaoImpl.getSysOpUserByOperId(UserId)
				.getOperBranchNo();
		// PCPSUtil.Log("sysbranch=="+sysbranch+"tiso.getToIssueBranchNo()==="+tiso.getToIssueBranchNo()+"tiso.getToSaleBranchNo()=="+tiso.getToSaleBranchNo());
		if (!((tiso.getToIssueBranchNo() != null && tiso.getToIssueBranchNo()
				.equals(sysbranch)) ^ (tiso.getToSaleBranchNo() != null && tiso
				.getToSaleBranchNo().equals(sysbranch)))) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "操作员并非接收方操作员");
		}

		// 得到调拨单明细
		List<TransferInOutStockOrderDetail> detaillist = TransferInOutStockOrderDetailDaoImpl
				.getTransferInOutStockOrderDetail(TransferInOutStockOrderID);
		if (ispass) {
			/*if(tiso.getConfirmOperId()==null){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "你没有权限操作该操作!");
			}*/
			if(tiso.getConfirmOperId()!=null){
				if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderID, UserId)>0){
					throw new CardManaApplicationException(
							CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
							"", "确认接收人和（创建人或调出审核人或接收人）不能是同一个人!请合理分配角色!");
				}
			}
			// 审批通过，修改卡所属机构，修改卡状态为正常，记录调拨单迁移明细，修改调拨单状态为确认接收，记录调拨单出入库明细
			// 修改卡状态为正常，所数据够为调入机构
			this.updateTransferInOutStockOrderDetailForCardStock(sysbranch,
					detaillist, tiso);
			// 修改调拨表，做调拨确认。
			tiso
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVEAUTH);
			tiso.setConfirmOperId(UserId);
			tiso.setConfirmTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tiso);

			// 记录调拨单状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tiso.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVEAUTH);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_CONFIRMATIONRECEIVE);
			toad.setRemark("接收确认");
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);

			// 日志记录
			sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
					SysOperLog.operType_Edit, "调拨单审批通过", "TransferOrderId:"
							+ toad.getTransferOrderId());
			// 记录出入库明细,需要记录一条出库，一条入库
			if (SysBranchDaoImpl.getSysBranchByBranchNo(sysbranch)
					.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH)) {
				this.insertIssueCardStockDetailList(
								"调拨入库确认",
								UserId,
								sysbranch,
								detaillist,
								tiso,
								IssueCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_IN,
								1);
			} else {
				this.insertSaleCardStockDetailList(
								"调拨入库确认",
								UserId,
								sysbranch,
								detaillist,
								tiso,
								SaleCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_IN,
								1);
			}
			//------------------2013-07-16 增加
			//记录一条出库的
			String frombranch=tiso.getFromIssueBranchNo()==null?tiso.getFromSaleBranchNo():tiso.getFromIssueBranchNo();
			
			
			if (SysBranchDaoImpl.getSysBranchByBranchNo(frombranch)
					.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH))
				this
						.insertIssueCardStockDetailList(
								"调拨出库确认",
								tiso.getOperNo(),
								frombranch,
								detaillist,
								tiso,
								IssueCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
								1);
			else
				this
						.insertSaleCardStockDetailList(
								"调拨出库确认",
								tiso.getTransferOrderId(),
								frombranch,
								detaillist,
								tiso,
								SaleCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_TRAN_OUT,
								1);
			
		} else {
			if(TransferOrderActionDetailDaoImpl.IsTransferOrderActionDetail(TransferInOutStockOrderID, UserId)>0){
				throw new CardManaApplicationException(
						CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "拒绝接收人和（创建人或调出审核人或接收人）不能是同一个人!请合理分配角色!");
			}
			// 审批不通过，记录调拨迁移明细，修改调拨单状态为拒收
			tiso
					.setTransferStatus(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION);
			tiso.setConfirmOperId(UserId);
			tiso.setConfirmTime(pcpsCommonDaoImpl.getDatabaseSysTime());

			TransferInOutStockOrderDaoImpl.updateTransferInOutStockOrder(tiso);

			// 记录状态迁移明细
			TransferOrderActionDetail toad = new TransferOrderActionDetail();
			toad.setTransferOrderId(tiso.getTransferOrderId());
			toad.setActionTime(pcpsCommonDaoImpl.getDatabaseSysTime());
			toad.setOperId(UserId);
			toad
					.setINITIALSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_RECEIVE);
			toad
					.setENDSTATUS(TransferInOutStockOrder.TRANSFERINOUTSTOCKORDER_TRANSFERSTATUS_REJECTION);
			toad
					.setActionType(TransferOrderActionDetail.TRANSFERORDERACTIONDETAIL_ACTIONTYPE_REFUSE);
			toad.setRemark(remark);
			TransferOrderActionDetailDaoImpl
					.insertTransferOrderActionDetail(toad);
			// 日志记录
			sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
					SysOperLog.operType_Edit, "调拨单审批不通过", "TransferOrderId:"
							+ toad.getTransferOrderId());
		}

	}

	/**
	 * @批量修改卡库存
	 * 
	 */
	private void updateTransferInOutStockOrderDetailForCardStock(
			String sysbranch, List<TransferInOutStockOrderDetail> detaillist,
			TransferInOutStockOrder tiso) throws PCPSApplicationException {
		TransferInOutStockOrderDetail tisod = null;
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			// 修改状态为正常,所属销售机构为调入机构
			CardStockDaoImpl.updateCardStockwithBranch(tisod.getBatchNo(),
					tisod.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
					CardStock.CARDSTOCK_STATUS_NORMAL, tiso
							.getToIssueBranchNo(), tiso.getToSaleBranchNo());
		}
	}

	/**
	 * @批量修改卡状态
	 * 
	 */
	private void updateTransferInOutStockOrderDetailForStatus(String sysbranch,
			List<TransferInOutStockOrderDetail> detaillist)
			throws PCPSApplicationException {
		TransferInOutStockOrderDetail tisod = null;
		for (int i = 0; i < detaillist.size(); i++) {
			tisod = detaillist.get(i);
			// 修改状态为正常,所属销售机构不变
			CardStockDaoImpl.updateCardStock(tisod.getBatchNo(), tisod
					.getCardStartSeqNo(), tisod.getCardEndSeqNo(),
					CardStock.CARDSTOCK_STATUS_NORMAL);
		}

	}
	/**
	 * 验证号段是否重复
	 */
	@SuppressWarnings("unchecked")
	private String isVoucherNo(String Voucherno, Map map) {
		Iterator ite = map.entrySet().iterator();
		while (ite.hasNext()) {
			Map.Entry<Object, Object> entry = (Entry<Object, Object>) ite
					.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			BigInteger bigkey = new BigInteger(key.toString());
			BigInteger bigvalue = new BigInteger(value.toString());
			BigInteger bigvoucherno = new BigInteger(Voucherno);
			if (bigvoucherno.intValue() >= bigkey.intValue()
					&& bigvoucherno.intValue() <= bigvalue.intValue())
				return "error";
		}
		return "";
	}
}
